package com.slipper.system.codeless.controller;

import com.slipper.constants.Constants;
import com.slipper.system.codeless.bo.ViewFolderBO;
import com.slipper.system.codeless.bo.ViewBO;
import com.slipper.system.codeless.service.ViewFolderService;
import com.slipper.system.codeless.service.ViewService;
import com.slipper.core.tree.TreeInfo;
import com.slipper.core.tree.TreeUtil;
import com.slipper.util.BeanUtil;
import com.slipper.util.ResultUtil;
import com.slipper.util.Tool;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 视图目录表
 */
@RestController
@RequestMapping("/system/viewFolder")
public class ViewFolderController {
    @Resource
    private ViewFolderService viewFolderService;
    @Resource
    private ViewService viewService;
    
    @PostMapping(value = "/queryBy")
    public String queryBy(@RequestBody ViewFolderBO bo) {
        if(Tool.isBlank(bo.getId())) {
            return ResultUtil.failureResult("数据获取失败！");
        }
        return ResultUtil.successResult(viewFolderService.queryBy(bo.getId()));
    }

    @PostMapping(value = "/queryList")
    public String queryList(@RequestBody ViewFolderBO bo) {
        return ResultUtil.successResult(viewFolderService.queryList(bo));
    }
    
    @PostMapping(value = "/queryPage")
    public String queryPage(@RequestBody ViewFolderBO bo) {
        return ResultUtil.successResult(viewFolderService.queryPage(bo));
    }

    @PostMapping(value = "/save")
    public String save(@RequestBody ViewFolderBO bo) {
        if (Tool.isBlank(bo.getParentId())) {
            bo.setParentId(Constants.ROOT_ID);
        }

        bo.setOperateId(Constants.getCurrentUser().getEmployeeId());
        return ResultUtil.saveResult(viewFolderService.save(bo), bo);
    }

    @PostMapping(value = "/delete")
    public String delete(@RequestBody ViewFolderBO bo) {
        if (Tool.isBlank(bo.getId())) {
            return ResultUtil.failureResult("参数错误！");
        }

        ViewFolderBO viewFolderBO = new ViewFolderBO();
        viewFolderBO.setParentId(bo.getId());
        if (viewFolderService.queryTotal(viewFolderBO) > 0) {
            return ResultUtil.failureResult("目录已被使用无法删除！");
        }

        ViewBO viewBO = new ViewBO();
        viewBO.setFolderId(bo.getId());
        if (viewService.queryTotal(viewBO) > 0) {
            return ResultUtil.failureResult("目录已被使用无法删除！");
        }
        
        return ResultUtil.deleteResult(viewFolderService.delete(bo));
    }


    @PostMapping("/queryTree")
    public String queryTree() {
        List<ViewFolderBO> folderList = viewFolderService.queryList(new ViewFolderBO());
        TreeInfo treeInfo = new TreeInfo("目录树");
        treeInfo.setRootId(Constants.ROOT_ID);
        treeInfo.setIdField("id");
        treeInfo.setPidField("parentId");
        treeInfo.setTextField("name");
        treeInfo.setSortField("ordinal");
        return ResultUtil.successResult(TreeUtil.buildTree(treeInfo, BeanUtil.setOrdinal(folderList), "asc"));
    }

    @PostMapping(value = "/updateParentId")
    public String updateParentId(@RequestBody ViewFolderBO bo) {
        if (Tool.isBlank(bo.getId())) {
            return ResultUtil.failureResult("参数错误！");
        }

        if (Tool.isBlank(bo.getParentId())) {
            bo.setParentId(Constants.ROOT_ID);
        }

        if (bo.getId().equals(bo.getParentId())) {
            return ResultUtil.failureResult("不能移动到此目录！");
        }

        if (viewFolderService.isParent(bo.getParentId(), bo.getId())) {
            return ResultUtil.failureResult("不允许移动到此子文件下");
        }

        bo.setOperateId(Constants.getCurrentUser().getEmployeeId());
        return ResultUtil.saveResult(viewFolderService.updateParentId(bo), bo);
    }
}
